package com.songqintuan.platform.foundation;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.sf.cglib.beans.BeanCopier;

import com.songqintuan.common.util.MessageFactory;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.common.util.ValidatorUtil;
import com.songqintuan.model.po.CustomerLevel;
import com.songqintuan.model.vo.CustomerLevelVo;
import com.songqintuan.persist.service.hibernate.ICustomerLevelService;
import com.songqintuan.platform.base.IManagePojoPageBean;
import com.songqintuan.platform.base.ManagePojoPageBean;
import com.songqintuan.platform.common.constant.PlatformConstants;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;
import com.songqintuan.platform.page.CustomerLevelPage;
import com.songqintuan.platform.util.EntityDataUtil;

@ManagedBean(name = "manageCustomerLevelPage")
@SessionScoped
public class ManageCustomerLevelPageBean extends ManagePojoPageBean implements
		IManagePojoPageBean, Serializable {



	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private CustomerLevelPage page;

	private CustomerLevelVo view;
	private CustomerLevelVo edit;

	public CustomerLevelPage getPage() {
		return page;
	}

	public void setPage(CustomerLevelPage page) {
		this.page = page;
	}

	public CustomerLevelVo getView() {
		return view;
	}

	public void setView(CustomerLevelVo view) {
		this.view = view;
	}

	public CustomerLevelVo getEdit() {
		return edit;
	}

	public void setEdit(CustomerLevelVo edit) {
		this.edit = edit;
	}

	public ManageCustomerLevelPageBean() {

		CustomerLevel obj = new CustomerLevel();
		page = new CustomerLevelPage(1, PlatformConstants.DEFAULT_ONE_PAGE_NUMBER, obj);
		page.getPageDatas();

	}

	public String selectPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (null != this.getPage().getPageNo()) {
			if (this.getPage().getPageNo().intValue() == Integer
					.valueOf(pageNo).intValue()) {
				return result;
			}
		}
		this.getPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getPage().getPageDatas();
		return result;

	}

	public String viewAction() {
		String result = null;
		clearErrorMessages();
		ICustomerLevelService<CustomerLevel, String> objService = (ICustomerLevelService<CustomerLevel, String>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.CUSTOMER_LEVEL_SERVICE_BEAN_NAME);
		objService.setEntityClass(CustomerLevel.class);
		objService.setPKClass(String.class);
		String objId = (String) SpringFacesUtil.getRequestParameter("objId");
		CustomerLevel obj = objService.findById(objId);
		CustomerLevelVo objVo = new CustomerLevelVo();
		BeanCopier copyHere = BeanCopier.create(CustomerLevel.class, CustomerLevelVo.class,
				false);
		copyHere.copy(obj, objVo, null);
		this.setAction(PlatformConstants.ACTION_VIEW_TYPE);
		this.setView(objVo);
		return result;
	}

	public String editAction() {// get data
		String result = null;
		clearErrorMessages();
		ICustomerLevelService<CustomerLevel, String> objService = (ICustomerLevelService<CustomerLevel, String>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.CUSTOMER_LEVEL_SERVICE_BEAN_NAME);
		objService.setEntityClass(CustomerLevel.class);
		objService.setPKClass(String.class);
		String objId = (String) SpringFacesUtil.getRequestParameter("objId");
		CustomerLevel obj = objService.findById(objId);
		CustomerLevelVo objVo = new CustomerLevelVo();
		BeanCopier copyHere = BeanCopier.create(CustomerLevel.class, CustomerLevelVo.class,
				false);
		copyHere.copy(obj, objVo, null);
		this.setAction(PlatformConstants.ACTION_EDIT_TYPE);
		this.setEdit(objVo);
		return result;
	}

	public String viewToEditAction() {
		String result = null;
		clearErrorMessages();
		this.setEdit(this.getView());
		this.setView(null);
		this.setAction(PlatformConstants.ACTION_EDIT_TYPE);
		return result;
	}

	public String updateAction() {
		String result = null;
		try {
			this.clearErrorMessages();
			validator(this.getErrorMessages(), this.getEdit());
			if (!(this.getErrorMessages().isEmpty())) {
				return null;
			}
			ICustomerLevelService<CustomerLevel, String> objService = (ICustomerLevelService<CustomerLevel, String>) SpringFacesUtil
					.getSpringBean(PlatformSpringBeanConstants.CUSTOMER_LEVEL_SERVICE_BEAN_NAME);
			objService.setEntityClass(CustomerLevel.class);
			objService.setPKClass(String.class);
			CustomerLevel obj = objService.findById(this.getEdit().getId());
			obj.setName(this.getEdit().getName());
			obj.setRemark(this.getEdit().getRemark());
			obj.setPointsGrowingLess(this.getEdit().getPointsGrowingLess());
			obj.setPointsDedicationLess(this.getEdit().getPointsDedicationLess());
			obj.setPointsExperienceLess(this.getEdit().getPointsExperienceLess());
			obj.setSort(this.getEdit().getSort());
			obj.setIsDelete(this.getEdit().getIsDelete());
			// 设置通用的修改数据属性 修改时间和修改人
			EntityDataUtil<CustomerLevel, Long> currUtil = new EntityDataUtil<CustomerLevel, Long>();
			currUtil.setModifyPropertyValue(obj);
			// end
			objService.saveOrUpdate(obj);
		} catch (Exception e) {
			String updateErrorN18 = MessageFactory.getMessage(
					PlatformConstants.USER_BUNDLE_NAME,
					"update.data.version.not.same.error.message",
					FacesMessage.SEVERITY_ERROR).getDetail();
			this.setEditDataInPageFlag(false);
			this.getErrorMessages().add(updateErrorN18);
			return result;
		} finally {
			// do nothing;//关闭session的时候还会报，并且在session那里抓取了所有的Exception。
		}
		return result;
	}

	public String preCreateAction() {
		String result = null;
		clearErrorMessages();
		this.setAction(PlatformConstants.ACTION_NEW_TYPE);
		CustomerLevelVo objVo = new CustomerLevelVo();
		objVo.setSort(1);
		objVo.setIsDelete(1);
		this.setEdit(objVo);
		return result;
	}

	public String saveAction() {// create new
		String result = null;
		clearErrorMessages();
		validator(this.getErrorMessages(), this.getEdit());
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		ICustomerLevelService<CustomerLevel, String> objService = (ICustomerLevelService<CustomerLevel, String>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.CUSTOMER_LEVEL_SERVICE_BEAN_NAME);
		objService.setEntityClass(CustomerLevel.class);
		objService.setPKClass(String.class);
		if (null != this.getEdit()
				&& !("".equals(this.getEdit().getCode()))) {
			CustomerLevel theObj = objService.findUniqueBy("code", this.getEdit().getCode());//
			if (null != theObj) {
				String systemUserExistErrorN18 = MessageFactory.getMessage(
						PlatformConstants.USER_BUNDLE_NAME,
						"customerlevel.code.exists.error.message",
						FacesMessage.SEVERITY_ERROR,
						this.getEdit().getCode()).getDetail();
				this.getErrorMessages().add(systemUserExistErrorN18);
			}
		}
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		CustomerLevel newObj = new CustomerLevel();
		BeanCopier copyHere = BeanCopier.create(CustomerLevelVo.class, CustomerLevel.class,
				false);
		copyHere.copy(this.getEdit(), newObj, null);
		// 设置通用的修改数据属性 修改时间和修改人
		try {
			EntityDataUtil<CustomerLevel, String> currUtil = new EntityDataUtil<CustomerLevel, String>();
			currUtil.setCreatePropertyValue(newObj);
		} catch (Exception e) {
			return result;
		} finally {
			// do nothing;//关闭session的时候还会报，并且在session那里抓取了所有的Exception。
		}
		// end
		objService.save(newObj);
		return result;
	}

	public String refreshPageAction() {
		String result = null;
		this.getPage().getPageDatas();
		return result;
	}

	public String searchPageAction() {
		this.getPage().getPageDatas();
		this.setAction(PlatformConstants.ACTION_SEARCH_TYPE);
		this.setEdit(null);
		this.setView(null);
		return null;
	}

	private void validator(List<String> errorMessages, CustomerLevelVo eidt) {
		String loginNameLabelN18 = MessageFactory.getMessage(
				PlatformConstants.USER_BUNDLE_NAME,
				"customerlevel.code.display", FacesMessage.SEVERITY_ERROR)
				.getDetail();
		ValidatorUtil.EmptyValidatorForInput(errorMessages,
				edit.getCode(), loginNameLabelN18);
	}
}
